package com.yuerlive.rainbow.module.mine.presenter;

import com.yuerlive.rainbow.api.FansApi;
import com.yuerlive.rainbow.api.FansService;
import com.yuerlive.rainbow.api.SubscribeApi;
import com.yuerlive.rainbow.api.SubscribeService;
import com.yuerlive.rainbow.api.UserApi;
import com.yuerlive.rainbow.api.UserService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.MyFansData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/5/25.
 */

public class MyFansPresenter extends BasePresenterImpl implements MyFansContract.Presenter {
    private MyFansContract.View view;
    private FansService service;
    private UserService userService;
    private SubscribeService subscribeService;
    private int page;

    public MyFansPresenter(MyFansContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        service = FansApi.getInstance().fansService;
        userService = UserApi.getInstance().service;
        subscribeService = SubscribeApi.getInstance().service;
    }

    @Override
    public void getMyFans(boolean isLoadMore) {
        if (isLoadMore) {
            page++;
        } else {
            page = 1;
        }
        view.showLoading();
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();

        params.put("page", String.valueOf(page));
        service.getMyFans(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<MyFansData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<MyFansData> myFansDataBaseData) {
                        view.hideLoading();
                        if (myFansDataBaseData != null && myFansDataBaseData.getObject() != null) {
                            view.showFansList(myFansDataBaseData.getObject().getList(), page);
                        }
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void subscribeUp(int pos, String upUserId) {
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();

        params.put("upUserId", upUserId);
        subscribeService.subscribe(params).compose(RxHelper.toUI()).subscribe(new RxObserver<BaseData>(this) {
            @Override
            protected void onSuccess(BaseData baseData) {
                view.showSubscribeResult(baseData.getCode(), pos);
            }

            @Override
            protected void onFail(String message) {

            }
        });

    }

    @Override
    public void getUserInfo(int pos, String userId) {
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();
        params.put("otherId", userId);
        userService.getUserInfo(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<UserInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<UserInfo> data) {
                        view.showUserInfo(data, pos);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }
}
